﻿Imports MyGeneration.dOOdads
Imports ClassSigeco
Imports System.Text
Imports System.Security.Cryptography

Public Class oiml

    'Public Shared dbConn As String = "Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Data Source=DGO-PC\SQLEXPRESS;Initial catalog=SistemaRC"
    Public Shared dbConn As String = Global.Sigeco.My.MySettings.Default.dbConn
    '"Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Data Source=localhost\SQLExpress;Initial catalog=SistemaRCV2"

    'System.Configuration.ConfigurationManager.AppSettings("dbConn")
    Public Shared idUsuario As Integer = 1
    Public Shared idTipoUsuario As String = ""
    Public Shared idMovimientoCajaActual As Integer = 0
    Public Shared Usuario As String = "Admin"
    Public Shared UsuarioNombre As String = "Admin"
    Public Shared idMotivoVenta As Integer = 2
    Public Shared codEstadoCierreNota As Integer = 6
    Public Shared codEstadoProduccionNota As Integer = 2
    Public Shared codEstadoAnulaNota As Integer = 11
    Public Shared idMotivoRecepcion As Integer = 6

    Public Shared Function ControlPermiso(ByVal idUsuario As Integer, ByVal Funcion As String) As Boolean
        'Dim usu As New Controller.
        'If usu.controlAccesosPermisos(dbConn, idUsuario, Funcion) Then
        Return True
        'Else
        '    Return False
        'End If
    End Function

    Private Function ValidateSHA1HashData(ByVal inputData As String, ByVal storedHashData As String) As Boolean
        'hash input text and save it string variable
        Dim getHashInputData As String = GetSHA1HashData(inputData)

        If String.Compare(getHashInputData, storedHashData) = 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Function GetSHA1HashData(ByVal data As String) As String
        'create new instance of md5
        Dim sha1__1 As SHA1 = SHA1.Create()

        'convert the input text to array of bytes
        Dim hashData As Byte() = sha1__1.ComputeHash(Encoding.[Default].GetBytes(data))

        'create new instance of StringBuilder to save hashed data
        Dim returnValue As New StringBuilder()

        'loop for each byte and add it to StringBuilder
        For i As Integer = 0 To hashData.Length - 1
            returnValue.Append(hashData(i).ToString())
        Next

        ' return hexadecimal string
        Return returnValue.ToString()
    End Function

    Private Shared _MessageError As Exception = Nothing
    Public Shared Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(ByVal value As Exception)
            _MessageError = value
        End Set
    End Property

    Public Shared Function Insert_Log(ByVal descripcion As String, ByVal codUsuario As Integer, _
                                ByVal actual As Boolean, Optional ByVal dt As DataTable = Nothing) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim datos As String = ""
            If Not dt Is Nothing Then
                For x As Integer = 0 To dt.Rows.Count - 1
                    For i As Integer = 0 To dt.Columns.Count - 1
                        datos += dt.Columns(i).ColumnName & ": " & dt.Rows(x)(i).ToString() & " | " '& Environment.NewLine
                    Next
                Next
            End If

            Dim usu As New ClassSigeco.ClassSigeco.sys_log(dbConn)

            usu.AddNew()
            usu.Descripcion = descripcion
            usu.Fecha = DateTime.Now
            usu.RealizadoPor = codUsuario

            If actual Then
                usu.ValorNuevo = datos
                usu.ValorAnterior = ""
            Else
                usu.ValorNuevo = ""
                usu.ValorAnterior = datos
            End If

            usu.Save()

            tx.CommitTransaction()

            Return usu.IdLog

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Shared Sub CargarCombos(ByRef ddl As ComboBox, ByVal display As String, ByVal value As String, ByVal dt As DataTable)
        If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
            ddl.DataSource = dt
            ddl.DisplayMember = display
            ddl.ValueMember = value

        Else

            ddl.DataSource = Nothing

        End If
    End Sub

    Public Shared Function isNumeric(ByVal valor As String) As Decimal
        Try
            Return Decimal.Parse(valor)
        Catch ex As Exception
            Return 0
        End Try
    End Function

End Class
